<?php
    include 'include/function.php';
    valid_user();
	$errorMsg = '';
	$source_len = 0;

	if (empty($_POST['as_problem']))
		$errorMsg = 'Please choose a problem';
	else if (empty($_POST['language']))
		$errorMsg = 'Please choose your language';
	else if (($source_len = strlen($_POST['source'])) > 200000)
		$errorMsg = 'Source is too long';
    else if (empty($_POST['contest_id']))
        $errorMsg = 'No such contest';
	if ($errorMsg) {
        $errorMsg = urlencode($errorMsg);
		header("Location: error?error=" . urlencode("$errorMsg"));
		exit;
	}
	if (submit($_POST['contest_id'], $_SESSION['user_id'], $_POST['as_problem'], $_POST['language'], date("Y-m-d H:i:s", time()), $_POST['source']))
		header("Location: status?contest_id=" . $_POST['contest_id']);
	else
		header("Location: error?error=" . urlencode("You cannot submit now"));
?>
